package com.hainatong.mvvmbase.http.interceptor;

import android.text.TextUtils;
import com.hainatong.mvvmbase.application.RootApplication;
import com.hainatong.mvvmbase.utils.L;
import com.hainatong.mvvmbase.utils.SharePreferenceUtil;
import com.hainatong.mvvmbase.utils.TokenSPUtils;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class HeaderInterceptor implements Interceptor
{

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException
    {

        //SharePreferenceUtil.getInstance(RootApplication.getInstance()).getAuthToken("");
        String accessToken = TokenSPUtils.getSUPToken(RootApplication.getInstance(),"gateway_token");

        String url  = chain.request().url().toString();

        Request.Builder builder = chain.request().newBuilder();

        builder.addHeader("Content-type","application/json;charset=UTF-8");
        if( url.contains("api/User/login"))
        {
            //L.e("HeaderInterceptor", "当前请求 登录请求" );
        }
        else
        {
            if(!TextUtils.isEmpty( accessToken ))
            {
                builder.addHeader("Authorization","Bearer "+accessToken);
            }
        }
        Request newRequest = builder.build();
        return chain.proceed( newRequest );
    }
}
